<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\model\RecordModel;
use app\common\model\VisitorModel;
use app\common\model\StaffModel;

class Index extends BaseController
{
    public function index()
    {
        //访问量
        $record = [];
        $record['total'] = RecordModel::where('firm_id',$this->request->firm_id)->count() ?? 0;
        $record['today'] = RecordModel::where('firm_id',$this->request->firm_id)->whereDay('create_time')->count() ?? 0;
        $record['month'] = RecordModel::where('firm_id',$this->request->firm_id)->whereMonth('create_time')->count() ?? 0;
        //访客量
        $visitor = [];
        $visitor['total'] = VisitorModel::where('firm_id',$this->request->firm_id)->count() ?? 0;
        $visitor['today'] = VisitorModel::where('firm_id',$this->request->firm_id)->whereDay('create_time')->count() ?? 0;
        $visitor['month'] = VisitorModel::where('firm_id',$this->request->firm_id)->whereMonth('create_time')->count() ?? 0;
        //员工
        $staff = [];
        $staff['total'] = StaffModel::where('firm_id',$this->request->firm_id)->count() ?? 0;
        $staff['today'] = StaffModel::where('firm_id',$this->request->firm_id)->whereDay('create_time')->count() ?? 0;
        $staff['month'] = StaffModel::where('firm_id',$this->request->firm_id)->whereMonth('create_time')->count() ?? 0;
        // 访客列表
        $visitor_log=[];
        $visitor_log['day'] = RecordModel::with(['user'])
            ->field('user_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('user_id')
            ->whereDay('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $visitor_log['week'] = RecordModel::with(['user'])
            ->field('user_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('user_id')
            ->whereWeek('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $visitor_log['month'] = RecordModel::with(['user'])
            ->field('user_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('user_id')
            ->whereMonth('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $visitor_log['year'] = RecordModel::with(['user'])
            ->field('user_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('user_id')
            ->whereYear('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        
        // 优秀员工
        
        $staff_log=[];
        $staff_log['day'] = RecordModel::with(['staff'])
            ->field('staff_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('staff_id')
            ->whereDay('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $staff_log['week'] = RecordModel::with(['staff'])
            ->field('staff_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('staff_id')
            ->whereWeek('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $staff_log['month'] = RecordModel::with(['staff'])
            ->field('staff_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('staff_id')
            ->whereMonth('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        $staff_log['year'] = RecordModel::with(['staff'])
            ->field('staff_id,count(record_id) as count')
            ->where('firm_id',$this->request->firm_id)
            ->group('staff_id')
            ->whereYear('create_time')
            ->order('count desc')
            ->limit(10)->page(0)
            ->select() ?? [];
        
        $return=[
            'record' => $record,
            'staff' => $staff,
            'visitor' => $visitor,
            
            'staff_log' => $staff_log,
            'visitor_log' => $visitor_log,
        ];
        return success($return);
    }
}
